﻿$(document).ready(function () {
    var eventID = 0;
    var eventName = "";
    $.ajax({
        type: "post",
        data: { action: "GetList" },
        dataType: "json",
        url: "../../Ajax/EventAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            var json = data.json;
            var tbody = "";
            $.each(json, function (k, v) {
                if (k == 0) { eventID = v.ID; eventName = v.Name }
                tbody += "<tr>"
                tbody += "<td><lable name='lblName' id='" + v.ID + "'>";
                tbody += v.Name;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblChangeNum' >";
                tbody += v.ChangeNum
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblTypeName' >";
                tbody += v.TypeName;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblIsTaskCondition' >";
                tbody += v.IsTaskCondition;
                tbody += "</lable></td>";
                tbody += "<td>";
                tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'><input type='button' onclick='SeltMould(" + v.ID + ",\"" + v.Name + "\")' value='查看概率模板'>"
                tbody += "</td>";
                tbody += "</tr>"
            })
            $("#tb1").empty().append(tbody)

            if (eventID != 0) SeltMould(eventID, eventName);
        },
        complete: function () {
        },
        error: function () {
        }
    });

    $("#btnAddMould").click(function () {
        EditMould(null);
    })

    $("#btnAdd").click(function () {
        Edit(null);
    })
})

function SeltMould(eventID, eventName) {
    $("#tblMould").attr("name", eventID)
    $("#lblMould").text(eventName);
    $.ajax({
        type: "post",
        data: { action: "GetList", eventID: eventID },
        dataType: "json",
        url: "../../Ajax/EventMouldAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            var json = data.json;
            var tbody = "";
            $.each(json, function (k, v) {
                tbody += "<tr>"
                tbody += "<td><lable name='lblName' id='" + v.ID + "'>";
                tbody += v.Name;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblSortNum'>";
                tbody += v.SortNum;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblClientToButt'>";
                tbody += v.ClientToButt;
                tbody += "</lable></td>";
                tbody += "<td>";
                tbody += "<input type='button' onclick='UpdateMould(this)' value='更新'><input type='button' onclick='DeleteMould(this)' value='删除'>"
                tbody += "</td>";
                tbody += "</tr>"
            })
            $("#tb2").empty().append(tbody)
        },
        complete: function () {
        },
        error: function () {
        }
    });
}

function UpdateMould(contr) {
    var lbl = $(contr).parent().parent().find("lable[name=lblName]").first();
    EditMould(lbl);
}

function DeleteMould(contr) {
    var lbl = $(contr).parent().parent().find("lable[name=lblName]").first();
    if (window.confirm("确认删除？")) {
        $.ajax({
            type: "post",
            data: { action: "Delete", id: $(lbl).attr("id") },
            async: true,
            url: "../../Ajax/EventMouldAjax.ashx",
            beforeSend: function () {
            },
            success: function (data) {
                if (data == "True") {
                    $($(lbl).parent().parent()).remove();
                    $.dialog.tips("删除成功！", 1);
                }
                else {
                    $.dialog.tips("删除失败！", 1);
                }
            },
            complete: function () {
            },
            error: function () {
            }
        })
    }
}

function EditMould(contr) {
    //lbl 是判断是添加还是修改  添加为null
    if ($.trim($("#tblMould").attr("name")) == "") {
        $.dialog.alert("请输入选择事件模板！");
        return false;
    }
    var content = "";
    if (contr != null) {
        content = "<table><tr><td>事件概率模板名称：<input type='text' id='txtName' value='" + $(contr).text() + "' style='width:200px;float:right' /></td></tr>"
                + "<tr><td>排序号：<input type='text' id='txtSortNum' value='" + $(contr).parent().parent().find("lable[name=lblSortNum]").text() + "' style='width:200px;float:right' /></td></tr>"
                + "<tr><td>对接号：<input type='text' id='txtClientToButt' value='" + $(contr).parent().parent().find("lable[name=lblClientToButt]").text() + "' style='width:200px;float:right' /></td></tr></table>";
    }
    else
        content = "<table><tr><td>事件概率模板名称：<input type='text' id='txtName' style='width:200px;float:right' /></td></tr>"
                + "<tr><td>排序号：<input type='text' id='txtSortNum' style='width:200px;float:right' /></td></tr>"
                + "<tr><td>对接号：<input type='text' id='txtClientToButt' style='width:200px;float:right' /></td></tr></table>";
    $.dialog({
        title: "更新事件概率模板",
        content: content,
        button: [
                    {
                        name: "提交",
                        callback: function () {
                            var objectName = $("#txtName").val();
                            if ($.trim(objectName) == "") {
                                $.dialog.alert("请输入事件名称！");
                                return false;
                            }

                            AddMould(contr, $(txtName));
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
        lock: true,
        drag: false
    });
}

function AddMould(contr, txtName) {
    var sortNum = $(txtName).parent().parent().parent().find("#txtSortNum").val();
    var clientToButt = $(txtName).parent().parent().parent().find("#txtClientToButt").val();
    var data = contr == null ? { action: "Add", name: $(txtName).val(), eventID: $(tblMould).attr("name"), sortNum: sortNum, clientToButt: clientToButt} :
              { action: "Update", id: $(contr).attr("id"), name: $(txtName).val(), sortNum: sortNum, clientToButt: clientToButt };
    $.ajax({
        type: "post",
        data: data,
        async: true,
        url: "../../Ajax/EventMouldAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != "") {
                if (contr == null) {

                    var tbody = "<tr>";
                    tbody += "<td><lable name='lblName' id='" + data + "'>";
                    tbody += $(txtName).val();
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblSortNum'>";
                    tbody += sortNum;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblClientToButt'>";
                    tbody += clientToButt;
                    tbody += "</lable></td>";
                    tbody += "<td>";
                    tbody += "<input type='button' onclick='UpdateMould(this)' value='更新'><input type='button' onclick='DeleteMould(this)' value='删除'>"
                    tbody += "</td>";
                    tbody += "</tr>"
                    $("#tb2").append(tbody);
                }
                else {
                    $(contr).text($(txtName).val());
                    $(contr).parent().parent().find("lable[name=lblSortNum]").text(sortNum);
                    $(contr).parent().parent().find("lable[name=lblClientToButt]").text(clientToButt);
                }
                $.dialog.tips("更新成功！", 1);
            }
            else $.dialog.tips("更新失败！", 1);
        },
        complete: function () {
        },
        error: function () {
        }
    })
}


function Update(contr) {
    var lbl = $(contr).parent().parent().find("lable[name=lblName]").first();
    Edit(lbl);
}

function Delete(contr) {
    var lbl = $(contr).parent().parent().find("lable[name=lblName]").first();
    if (window.confirm("确认删除？")) {
        $.ajax({
            type: "post",
            data: { action: "Delete", id: $(lbl).attr("id") },
            async: true,
            url: "../../Ajax/EventAjax.ashx",
            beforeSend: function () {
            },
            success: function (data) {
                if (data == "True") {
                    $($(lbl).parent().parent()).remove();
                    $.dialog.tips("删除成功！", 1);
                    $("#btnAddMould").attr("name", "");
                    $("#lblMould").text("");
                    $("#tb2").empty();
                }
                else {
                    $.dialog.tips("删除失败！", 1);
                }
            },
            complete: function () {
            },
            error: function () {
            }
        })
    }
}

function Edit(contr) {
    //lbl 是判断是添加还是修改  添加为null
    var content = "";
    if (contr != null) {
        var changeNum = $(contr).parent().parent().find("lable[name=lblChangeNum]").first();
        var typeName = $(contr).parent().parent().find("lable[name=lblTypeName]").first();
        var isTaskCondition = $(contr).parent().parent().find("lable[name=lblIsTaskCondition]").first().text();
        content += "<table><tr><td>事件名称：<input type='text' id='txtName' value='" + $(contr).text() + "' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>更改概率值：<input type='text' id='txtChangeNum' value='" + $(changeNum).text() + "' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>对接名称：<input type='text' id='txtTypeName' value='" + $(typeName).text() + "' style='width:200px;float:right' /></td></tr>"
        if (isTaskCondition == "True")
            content += "<tr><td>是否任务条件：<input type='radio' name='radioIsTaskCondition' id='radioisTure' value='True' checked='checked' /><lable>是</lable><input type='radio' name='radioIsTaskCondition' id='radioisfalse' value='False'/><lable>否</lable></td></tr>";
        else
            content += "<tr><td>是否任务条件：<input type='radio' name='radioIsTaskCondition' id='radioisTure' value='True' /><lable>是</lable><input type='radio' name='radioIsTaskCondition' id='radioisfalse' value='False' checked='checked'/><lable>否</lable></td></tr>";
        content += "</table>";
    }
    else {
        content += "<table><tr><td>事件名称：<input type='text' id='txtName' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>更改概率值：<input type='text' id='txtChangeNum' value='' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>对接名称：<input type='text' id='txtTypeName' value='' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>是否任务条件：<input type='radio' name='radioIsTaskCondition' id='radioisTure' value='True' /><lable>是</lable><input type='radio' name='radioIsTaskCondition' id='radioisfalse' value='False' checked='checked'/><lable>否</lable></td></tr>";
        content += "</table>";
    }
    $.dialog({
        title: "更新事件",
        content: content,
        button: [
                    {
                        name: "提交",
                        callback: function () {
                            var objectName = $("#txtName").val();
                            if ($.trim(objectName) == "") {
                                $.dialog.alert("请输入事件名称！");
                                return false;
                            }
                            if ($.trim($("#txtTypeName").val()) == "") {
                                $.dialog.alert("请输入对接名称！");
                                return false;
                            }
                            Add(contr, $(txtName));
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
        lock: true,
        drag: false
    });
}

function Add(contr, txtName) {
    var typeName = $(txtName).parent().parent().parent().find("#txtTypeName").val();
    var changeNum = $(txtName).parent().parent().parent().find("#txtChangeNum").val();
    var isTaskCondition = $(txtName).parent().parent().parent().find("input[name=radioIsTaskCondition]:checked").val();
    var data = contr == null ? { action: "Add", name: $(txtName).val(), changeNum: changeNum, typeName: typeName, isTaskCondition: isTaskCondition} :
              { action: "Update", id: $(contr).attr("id"), name: $(txtName).val(), changeNum: changeNum, typeName: typeName, isTaskCondition: isTaskCondition };
    $.ajax({
        type: "post",
        data: data,
        async: true,
        url: "../../Ajax/EventAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != "") {
                if (contr == null) {
                    var tbody = "<tr>";
                    tbody += "<td><lable name='lblName' id='" + data + "'>";
                    tbody += $(txtName).val();
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblChangeNum' >";
                    tbody += changeNum
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblTypeName' >";
                    tbody += typeName;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblIsTaskCondition' >";
                    tbody += isTaskCondition;
                    tbody += "</lable></td>";
                    tbody += "<td>";
                    tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'><input type='button' onclick='SeltMould(" + data + ",\"" + $(txtName).val() + "\")' value='查看概率模板'>"
                    tbody += "</td>";
                    tbody += "</tr>"
                    $("#tb1").append(tbody);
                }
                else {
                    $(contr).text($(txtName).val());
                    $(contr).parent().parent().find("lable[name=lblChangeNum]").first().text(changeNum);
                    $(contr).parent().parent().find("lable[name=lblTypeName]").first().text(typeName);
                    $(contr).parent().parent().find("lable[name=lblIsTaskCondition]").first().text(isTaskCondition);
                }
                $.dialog.tips("更新成功！", 1);
            }
            else $.dialog.tips("更新失败！", 1);
        },
        complete: function () {
        },
        error: function () {
        }
    })
}